package com.hibase.core.rule.config;

import com.hibase.core.rule.annotation.HibaseRuleMethod;
import com.hibase.core.rule.entity.RuleMethod;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.LinkedList;

/**
 *
 * @author chenfeng
 * @version 1.0
 * @date 2021/11/15 10:10
 * @des https://gitee.com/cheershi
 */
@Component
public class RuleMethodProcessor implements BeanPostProcessor {
    // subscriber cache
    public static final LinkedList<RuleMethod> RULE_METHODS = new LinkedList<>();

    @Value("${hibase.rule.disable:false}")
    private Boolean disable;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (disable == null || !disable) {
            Method[] methods = bean.getClass().getMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(HibaseRuleMethod.class)) {
                    RULE_METHODS.add(RuleMethod.of(bean, method));
                }
            }
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
